from flask import Flask, request, render_template
from storage import Storage
from ai_assistant import AIAssistant
from learning import LearningModule
from forum import ForumModule
from entertainment import EntertainmentModule
from job_market import JobMarketModule

app = Flask(__name__)
storage = Storage('data.db')
ai_assistant = AIAssistant()

# 实例化模块
learning_module = LearningModule(storage, ai_assistant)
forum_module = ForumModule(storage)
entertainment_module = EntertainmentModule()
job_market_module = JobMarketModule(storage)

@app.route('/')
def home():
    return render_template('index.html')

@app.route('/forum')
def forum():
    posts = forum_module.get_posts()
    return render_template('forum.html', posts=posts)

@app.route('/jobs')
def jobs():
    return render_template('jobs.html')

@app.route('/game')
def game():
    return render_template('game.html')

# 学习功能
@app.route('/create_learning', methods=['POST'])
def create_learning():
    data = request.json
    return learning_module.create_learning_content(data['content'], data['author'])

# 论坛功能
@app.route('/forum/post', methods=['POST'])
def create_post():
    data = request.json
    forum_module.create_post(data['content'], data['author'])
    return {"message": "帖子已创建"}, 201

@app.route('/forum/posts', methods=['GET'])
def get_forum_posts():
    posts = forum_module.get_posts()
    return {"posts": posts}, 200

# 娱乐功能
@app.route('/entertainment/joke', methods=['GET'])
def get_joke():
    joke = entertainment_module.get_random_joke()
    return {"joke": joke}, 200

# 自由职业功能
@app.route('/job', methods=['POST'])
def create_job():
    data = request.json
    return job_market_module.create_job_post(data['description'], data['bounty'], data['creator'])

@app.route('/job/apply', methods=['POST'])
def apply_job():
    data = request.json
    return job_market_module.apply_for_job(data['job_id'], data['applicant'])

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)
